Coverage Report

Created: 2024-12-26 12:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
D:\a\tools.proto\tools.proto\compiler\src\gen\rust\core.rs
Line
Count
Source
1
// Copyright (c) 2024, BlockProject 3D
2
//
3
// All rights reserved.
4
//
5
// Redistribution and use in source and binary forms, with or without modification,
6
// are permitted provided that the following conditions are met:
7
//
8
//     * Redistributions of source code must retain the above copyright notice,
9
//       this list of conditions and the following disclaimer.
10
//     * Redistributions in binary form must reproduce the above copyright notice,
11
//       this list of conditions and the following disclaimer in the documentation
12
//       and/or other materials provided with the distribution.
13
//     * Neither the name of BlockProject 3D nor the names of its contributors
14
//       may be used to endorse or promote products derived from this software
15
//       without specific prior written permission.
16
//
17
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29
use crate::codec_map_initializer;
30
use crate::compiler::Protocol;
31
use crate::gen::base::Error;
32
use crate::gen::codec::CodecMap;
33
use crate::gen::file::{Content, File, FileType};
34
use crate::gen::rust::message::{
35
    gen_message_decl, gen_message_from_slice_impl, gen_message_offsets_decl, gen_message_write_impl,
36
};
37
use crate::gen::rust::params::Params;
38
use crate::gen::rust::r#enum::gen_enum_decl;
39
use crate::gen::rust::structure::gen_structure_decl;
40
use crate::gen::rust::union::gen_union_decl;
41
use crate::gen::template::Template;
42
use crate::gen::Generator;
43
use bp3d_debug::trace;
44
use std::path::Path;
45
46
const TEMPLATE_CODEC_BASE: &[u8] = include_bytes!("./default_codec/base.template");
47
const TEMPLATE_CODEC_STRING: &[u8] = include_bytes!("./default_codec/string.template");
48
const TEMPLATE_CODEC_LIST: &[u8] = include_bytes!("./default_codec/list.template");
49
50
pub struct GeneratorRust;
51
52
impl Generator for GeneratorRust {
53
    type Error = Error;
54
    type Params<'a> = Params<'a>;
55
56
3
    fn get_default_codecs<'fragment, 'variable>() -> CodecMap<'fragment, 'variable> {
57
3
        let mut codecs = CodecMap::new(codec_map_initializer! {
58
3
            "base" => TEMPLATE_CODEC_BASE
59
3
        });
60
3
        codecs.insert(
61
3
            "string",
62
3
            Template::compile_with_includes(TEMPLATE_CODEC_STRING, &codecs).unwrap(),
63
3
        );
64
3
        codecs.insert(
65
3
            "list",
66
3
            Template::compile_with_includes(TEMPLATE_CODEC_LIST, &codecs).unwrap(),
67
3
        );
68
3
        codecs
69
3
    }
70
71
37
    fn generate(proto: &Protocol, codec_map: &CodecMap, params: &Params) -> Result<Vec<File>, Self::Error> {
72
37
        trace!({?params}, "Generating protocol {}", proto.full_name);
73
37
        let decl_messages_code =
74
37
            proto.messages.iter().map(|v| 
gen_message_decl(v, codec_map, &proto.type_path_map, params)31
);
75
37
        let impl_from_slice_messages_code =
76
37
            proto.messages.iter().map(|v| 
gen_message_from_slice_impl(v, codec_map, &proto.type_path_map)31
);
77
37
        let impl_write_messages_code =
78
37
            proto.messages.iter().map(|v| 
gen_message_write_impl(v, codec_map, &proto.type_path_map, params)31
);
79
60
        let decl_structures = proto.structs.iter().map(|v| gen_structure_decl(v, &proto.type_path_map, params));
80
37
        let decl_enums = proto.enums.iter().map(|v| 
gen_enum_decl(v)6
);
81
37
        let decl_unions = proto.unions.iter().map(|v| 
gen_union_decl(v, &proto.type_path_map, params)6
);
82
37
        let mut files = vec![
83
37
            File::new(
84
37
                FileType::Message,
85
37
                "messages.rs",
86
37
                &Content::try_from_iter(decl_messages_code)
?0
,
87
            ),
88
            File::new(
89
37
                FileType::MessageReading,
90
37
                "messages_from_bytes.rs",
91
37
                &Content::try_from_iter(impl_from_slice_messages_code)
?0
,
92
            ),
93
            File::new(
94
37
                FileType::MessageWriting,
95
37
                "messages_write.rs",
96
37
                &Content::try_from_iter(impl_write_messages_code)
?0
,
97
            ),
98
37
            File::new(
99
37
                FileType::Structure,
100
37
                "structures.rs",
101
37
                &Content::from_iter(decl_structures),
102
37
            ),
103
37
            File::new(FileType::Enum, "enums.rs", &Content::from_iter(decl_enums)),
104
37
            File::new(FileType::Union, "unions.rs", &Content::from_iter(decl_unions)),
105
37
        ];
106
37
        if params.enable_message_offsets {
  Branch (106:12): [True: 1, False: 36]
  Branch (106:12): [Folded - Ignored]
107
1
            let decl_messages_code_offsets =
108
2
                proto.messages.iter().map(|v| gen_message_offsets_decl(v, codec_map, &proto.type_path_map));
109
1
            files.push(File::new(
110
1
                FileType::MessageReading,
111
1
                "messages_offsets.rs",
112
1
                &Content::try_from_iter(decl_messages_code_offsets)
?0
,
113
            ));
114
36
        }
115
37
        Ok(files)
116
37
    }
117
118
37
    fn generate_umbrella<'a>(
119
37
        proto_name: &str,
120
37
        files: impl Iterator<Item = &'a Path>,
121
37
        _: &Params,
122
37
    ) -> Result<String, Self::Error> {
123
37
        let mut code = format!("pub mod {} {{\n", proto_name);
124
133
        for 
file96
in files {
125
96
            code += &format!("include!({:?});\n", file);
126
96
        }
127
37
        code += "}\n";
128
37
        Ok(code)
129
37
    }
130
131
258
    fn get_language_extension() -> &'static str {
132
258
        "rs"
133
258
    }
134
}